package co.edu.usbcali.unidades.utilities;


import java.io.File;
import java.security.Security;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.InitialContext;
import javax.servlet.http.HttpServlet;

@Stateless()
@WebService()
public class SendMailUtility extends HttpServlet{

    private static final long serialVersionUID = -6868389093189573328L;
	
	@Resource(name="mail/simac", mappedName="mail/simac")
	Session mailSession; // inject a Mail Session

	@WebMethod
    public void sendMail(
            String recipients[],
            String subject,
            String message,
            String from,
            File attachment) throws Exception{

        boolean debug = true;
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        
        InitialContext ctx = new InitialContext();
//        Object a = ctx.lookup("mail/simac");
//        Object b = ctx.lookup("java:comp/env/mail/simac");
//        System.out.println(a);
//        System.out.println(b);
        
        javax.mail.Session session = (javax.mail.Session)ctx.lookup("java:comp/env/mail/simac"); 
        
        session.setDebug(debug);
        //session.setPasswordAuthentication(session.getTransport().getURLName(), new SMTPAuthenticator().getPasswordAuthentication());
        
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        
//        msg.setContent(message,
//                "text/html; charset=ISO-8859-1");
//        Transport.send(msg);
        
        // Create the message part 
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message
        messageBodyPart.setText(message);

        // Create a multipar message
        Multipart multipart = new MimeMultipart();

        // Set text message part
        multipart.addBodyPart(messageBodyPart);

        // Part two is attachment
        if (attachment.isFile() && attachment.exists()){
	        messageBodyPart = new MimeBodyPart();
	        String filename = attachment.getName();
	        DataSource source = new FileDataSource(attachment);
	        messageBodyPart.setDataHandler(new DataHandler(source));
	        messageBodyPart.setFileName(filename);
	        multipart.addBodyPart(messageBodyPart);
        }

        // Send the complete message parts
        msg.setContent(multipart);

        // Send message
        Transport.send(msg);
        
    }
	
	

}
